﻿using System; 
using System.Text;
using System.Collections.Generic; 
using System.Data;
using Bangle.Common;
using Bangle.Model;
namespace Bangle.BLL {
	 	//T_Sale
		public partial class T_SaleBLL
	{
   		     
		private readonly Bangle.DAL.T_SaleDAL dal=new Bangle.DAL.T_SaleDAL();
		public T_SaleBLL()
		{}
		
		#region  Method
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(Guid SaleId)
		{
			return dal.Exists(SaleId);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void  Add(Bangle.Model.T_Sale model)
		{
						dal.Add(model);
						
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Bangle.Model.T_Sale model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid SaleId)
		{
			
			return dal.Delete(SaleId);
		}
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Bangle.Model.T_Sale GetModel(Guid SaleId)
		{
			
			return dal.GetModel(SaleId);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public Bangle.Model.T_Sale GetModelByCache(Guid SaleId)
		{
			
			string CacheKey = "T_SaleModel-" + SaleId;
			object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(SaleId);
					if (objModel != null)
					{
						int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
						Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Bangle.Model.T_Sale)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Bangle.Model.T_Sale> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}

        /// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Bangle.Model.T_Sale> DataTableToList(DataTable dt)
		{
			List<Bangle.Model.T_Sale> modelList = new List<Bangle.Model.T_Sale>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Bangle.Model.T_Sale model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Bangle.Model.T_Sale();					
																									if(dt.Rows[n]["SaleId"].ToString()!="")
				{
					model.SaleId= Guid.Parse(dt.Rows[n]["SaleId"].ToString());
				}
																								model.SaleCode= dt.Rows[n]["SaleCode"].ToString();
																																model.SaleTime= dt.Rows[n]["SaleTime"].ToString();
																																								if(dt.Rows[n]["CustomerId"].ToString()!="")
				{
					model.CustomerId= Guid.Parse(dt.Rows[n]["CustomerId"].ToString());
				}
																								model.Salesman= dt.Rows[n]["Salesman"].ToString();
																												if(dt.Rows[n]["ExchangeRate"].ToString()!="")
				{
					model.ExchangeRate=decimal.Parse(dt.Rows[n]["ExchangeRate"].ToString());
				}
																																if(dt.Rows[n]["Freight"].ToString()!="")
				{
					model.Freight=decimal.Parse(dt.Rows[n]["Freight"].ToString());
				}
																																if(dt.Rows[n]["PayFreight"].ToString()!="")
				{
					model.PayFreight=decimal.Parse(dt.Rows[n]["PayFreight"].ToString());
				}
																																				model.Payment= dt.Rows[n]["Payment"].ToString();
																												if(dt.Rows[n]["StateId"].ToString()!="")
				{
					model.StateId=int.Parse(dt.Rows[n]["StateId"].ToString());
				}
																																				model.Description= dt.Rows[n]["Description"].ToString();
																												if(dt.Rows[n]["CreateUserId"].ToString()!="")
				{
					model.CreateUserId=int.Parse(dt.Rows[n]["CreateUserId"].ToString());
				}
																																				model.CreateTime= dt.Rows[n]["CreateTime"].ToString();
																												if(dt.Rows[n]["UpdateUserId"].ToString()!="")
				{
					model.UpdateUserId=int.Parse(dt.Rows[n]["UpdateUserId"].ToString());
				}
																																				model.UpdateTime= dt.Rows[n]["UpdateTime"].ToString();
																												if(dt.Rows[n]["Valid"].ToString()!="")
				{
					model.Valid=int.Parse(dt.Rows[n]["Valid"].ToString());
				}
																										
				
					modelList.Add(model);
				}
			}
			return modelList;
		}
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}
#endregion
   
	}
}